<template>
  <div class="app-container">
    <el-form ref="form" v-model="searchParams">
      <el-row>
        <el-col :span="8">
          <el-form-item label="工号" label-width="80px">
            <el-input v-model="searchParams.employeeId" placeholder="请输入工号"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="姓名" label-width="80px">
            <el-input v-model="searchParams.employeeName" placeholder="请输入姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item  style="margin-left: 10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
            <el-button style="margin-left: 10px" icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-button type="primary" icon="el-icon-confirm" size="mini" @click="submitChoose">提交</el-button>
        </el-col>
        <el-col :span="24">
          <el-table ref="userTable" :row-key="getRowKey" :data="employeeList" @selection-change="handleSelectedUser"
                    highlight-current-row>
            <el-table-column type="selection" :reserve-selection="true" width="55" align="enter"/>
            <el-table-column label="序号" align="center" type="index"/>
            <el-table-column label="工号" align="center" prop="employeeId"/>
            <el-table-column label="姓名" align="center" prop="employeeName"/>
          </el-table>
          <!-- 分页 -->
          <pagination
                  v-show="total>0"
                  :total="total"
                  :page.sync="pageParameter.page"
                  :limit.sync="pageParameter.rows"
                  @pagination="getList"
          />
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import {getDeptLeader} from "@/api/employee/employee"

  export default {
    props: {
      // 待分配证书列表
      toAllocateList: Array
    },
    data() {
      return {
        // 总条数
        total: 0,
        // 分页参数
        pageParameter: {
          page: 1,
          rows: 10
        },
        // 查询参数
        searchParams: {},
        // 人员列表
        employeeList: [],
        // 复选框选中列表
        selectedList: []
      }
    },
    created() {
      this.getList()
    },
    methods: {

      // 获取人员列表
      getList() {
        getDeptLeader({
          pageParameter: this.pageParameter,
          searchParams: this.searchParams
        }).then(response => {
          this.employeeList = response.responseBody.result
          this.total = response.responseBody.pageParameter.totalCount
        })
      },

      // 重置
      reset() {
        this.searchParams = {}
        this.getList()
      },

      // 复选框选中处理
      handleSelectedUser(item) {
        this.selectedList = item
      },

      getRowKey (row) {
        return row.id
      },

      // 取消
      cancel() {
        this.$emit('cancel')
      },

      // 提交
      submitChoose() {
        // 检查复选框是否为空
        if(this.selectedList.length === 0){
          this.$message.error('请选择领导后再进行提交')
          return
        }
        let leadId = '' // 领导工号
        let leadName = '' // 领导姓名
        this.selectedList.forEach(item => {
          leadId = leadId + item.employeeId + ','
          leadName = leadName + item.employeeName + ','
        })
        // 去掉末尾逗号
        leadId = leadId.slice(0, -1)
        leadName = leadName.slice(0, -1)
        // 为证书绑定领导
        this.toAllocateList.forEach(item => {
          item.leadId = leadId
          item.leadName = leadName
        })
        this.$emit('submitChoose')
      }
    }
  }
</script>
<style scoped>

</style>